//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import flash.events.Event;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.core.WindowManager;
	import reader.model.vo.BlogDraftVO;
	import reader.view.BlogWriterMediator;
	import reader.view.components.BlogWriter;

	public class OpenWriterCommand extends SimpleCommand implements ICommand
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var window:BlogWriter;
		private var draft:BlogDraftVO;
		
		////////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function execute(notification:INotification):void
		{
			var sharedDraft:BlogDraftVO = notification.getBody() as BlogDraftVO;
			if(!WindowManager.isWindowRegisted(ReaderModelLocator.WINDOW_WRITER_PANEL))
			{
				window = new BlogWriter();
				window.windowType = ReaderModelLocator.WINDOW_WRITER_PANEL;
				window.initView = notification.getType();
				WindowManager.registerWindow(window);
				window.addEventListener(FlexEvent.CREATION_COMPLETE, onWindowCreate_handler);
				window.addEventListener(Event.CLOSE, onCloseWindow_handler);
				window.open();
				if(sharedDraft) draft = sharedDraft;
			}
			else
			{
				window = WindowManager.getRegisterWindow(ReaderModelLocator.WINDOW_WRITER_PANEL) as BlogWriter;
				window.setViewStack(notification.getType());
				window.nativeWindow.orderToFront();
				if(sharedDraft)
					sendNotification(ApplicationFacade.EDIT_DRAFT, sharedDraft);
			}
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onWindowCreate_handler(e:Event):void
		{
			window.removeEventListener(FlexEvent.CREATION_COMPLETE, onWindowCreate_handler);
			
			facade.registerCommand(ApplicationFacade.INSERT_PHOTOS, InsertPhotosCommand);
			facade.registerCommand(ApplicationFacade.AUTOSAVE_SAVE, AutoSaveCommand);
			facade.registerCommand(ApplicationFacade.AUTOSAVE_READ, ReadAutoSaveCommand);
			facade.registerCommand(ApplicationFacade.AUTOSAVE_CLEAR, ClearAutoSaveCommand);
			facade.registerCommand(ApplicationFacade.OPEN_AUTH_WINDOW, OpenAuthCommand);
			facade.registerProxy(ReaderModelLocator.instance.sqliteProxyFactory.draftProxy);
			facade.registerMediator(new BlogWriterMediator(window));
			if(draft)
			{
				sendNotification(ApplicationFacade.EDIT_DRAFT, draft);
				draft = null;
			}
					
		}
		
		private function onCloseWindow_handler(e:Event):void
		{
			window.removeEventListener(Event.CLOSE, onCloseWindow_handler);
			facade.removeMediator(BlogWriterMediator.NAME);
			facade.removeProxy(ReaderModelLocator.instance.sqliteProxyFactory.draftProxyName);
			facade.removeCommand(ApplicationFacade.OPEN_AUTH_WINDOW);
			facade.removeCommand(ApplicationFacade.INSERT_PHOTOS);
			facade.removeCommand(ApplicationFacade.AUTOSAVE_SAVE);
			facade.removeCommand(ApplicationFacade.AUTOSAVE_READ);
			facade.removeCommand(ApplicationFacade.AUTOSAVE_CLEAR);
		}
		
	}
}